import { _decorator, Component, JsonAsset, Node, Prefab, resources, SpriteFrame } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('ResMgr')
export class ResMgr {

    static loadRes(url: string, callBack: Function) {
        resources.load(url, (err, res: any) => {
            if (err) {
                console.error(err);
                callBack && callBack(res, err);
                return;
            }
            callBack && callBack(res, null);

        });
    }

    static loadPrefab(url: string): Promise<any> {
        return new Promise((resolve, reject) => {
            this.loadRes(url, (res: any, err: Error) => {
                if (err) {
                    reject && reject();
                } else {
                    resolve && resolve(res as Prefab);
                }
            })
        })
    }

    static loadJson(url: string): Promise<any> {
        return new Promise((resolve, reject) => {
            this.loadRes(url, (res: any, err: Error) => {
                if (err) {
                    reject && reject();
                } else {
                    resolve && resolve(res as JsonAsset);
                }
            })
        })
    }

    static loadDir(path: string, type: typeof SpriteFrame = SpriteFrame) {
        return new Promise<SpriteFrame[]>((resolve, reject) => {
            resources.loadDir(path, type, function (err, res) {
                if (err) {
                    reject(err);
                    return;
                } else {
                    resolve(res);
                }
            });
        });
    }

}


